/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren.modules.project.controller;

import io.renren.common.annotation.SysLog;
import io.renren.common.utils.DateUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.project.entity.BackendEntity;
import io.renren.modules.project.entity.FrontendEntity;
import io.renren.modules.project.service.BackendService;
import io.renren.modules.project.service.FrontendService;
import io.renren.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 系统用户
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/backend")
public class BackendController extends AbstractController {
	@Autowired
	private BackendService backendService;


	/**
	 * 所有用户列表
	 */
	@GetMapping("/list")
	@RequiresPermissions("backend:list")
	public R list(@RequestParam Map<String, Object> params){
		PageUtils page = backendService.queryPage(params);

		return R.ok().put("page", page);
	}

	@GetMapping("/treelist")
	@RequiresPermissions("backend:list")
	public R treeList(@RequestParam Map<String, Object> params){
		PageUtils page = backendService.treeList(params);

		return R.ok().put("page", page);
	}


	@GetMapping("/treelog")
	@RequiresPermissions("runtimelog:list")
	public R treeLog(@RequestParam("id") long id){
		return R.ok().put("list", backendService.treeLog(id));
	}

	@GetMapping("/all")
	@RequiresPermissions("backend:list")
	public R listAll(@RequestParam Map<String, Object> params){
		List list = backendService.list();

		return R.ok().put("data", list);
	}

	/**
	 * 获取登录的用户信息
	 */
	@GetMapping("/info/{id}")
	public R info(@PathVariable("id") Long id){
		return R.ok().put("info", backendService.getById(id));
	}
	
	/**
	 * 保存用户
	 */
	@SysLog("保存用户")
	@PostMapping("/save")
	@RequiresPermissions("backend:save")
	public R save(@RequestBody BackendEntity entity){
		ValidatorUtils.validateEntity(entity, AddGroup.class);
		entity.setCreateTime(DateUtils.formatDefault(new Date()));
		backendService.saveEntity(entity);
		
		return R.ok();
	}
	
	/**
	 * 修改用户
	 */
	@SysLog("修改用户")
	@PostMapping("/update")
	@RequiresPermissions("frontend:update")
	public R update(@RequestBody BackendEntity user){
		ValidatorUtils.validateEntity(user, UpdateGroup.class);
		backendService.update(user);
		
		return R.ok();
	}
	
	/**
	 * 删除用户
	 */
	@SysLog("删除用户")
	@PostMapping("/delete")
	@RequiresPermissions("backend:delete")
	public R delete(@RequestBody Long[] delIds){
		backendService.deleteBatch(delIds);
		return R.ok();
	}

	@SysLog("删除用户")
	@PostMapping("/clearLog")
	@RequiresPermissions("runtimelog:clearLog")
	public R clearLog(@RequestParam("serverId") Long serverId,@RequestParam("logPath") String logPath){
		try{
			backendService.clearLog(serverId,logPath);
			return R.ok();
		}catch (Exception e){
			e.printStackTrace();
			return R.error("清除记录失败！");
		}

	}
	@SysLog("删除用户")
	@PostMapping("/showLogContent")
	@RequiresPermissions("runtimelog:list")
	public R showLogContent(@RequestParam("serverId") Long serverId,@RequestParam("logPath") String logPath
			,@RequestParam("page") Long page,@RequestParam("limit") Long limit,@RequestParam("toEnd") boolean toEnd){
		try{

			return R.ok().put("page",backendService.showLogContent(serverId,logPath,page,limit,toEnd));
		}catch (Exception e){
			e.printStackTrace();
			return R.error("清除记录失败！");
		}

	}


}
